﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Language
{
    public class LocalLanguage
    {
        private static Dictionary<string, Dictionary<string, string>> _languageData;
        private static string _currentLanguage = "zh"; // 默认语言为中文

        static LocalLanguage()
        {
            InitializeLanguageData();
        }

        private static void InitializeLanguageData()
        {
            _languageData = new Dictionary<string, Dictionary<string, string>>
            {
                // 中文语言包
                ["zh"] = new Dictionary<string, string>
                {
                    ["MessageTips1"] = "软件已经启动，请在后台确认。",
                    ["MessageTips2"] = "正在初始化日志系统...",
                    ["MessageTips3"] = "正在设置异常处理...",
                    ["MessageTips4"] = "正在读取配置...",
                    ["MessageTips5"] = "初始化完成",
                    ["MessageTips6"] = "致命错误",
                    ["MessageTips7"] = "程序出现大异常，请联系 【金源自动化软件设计部】。\r\n异常信息: {0}\r\n异常堆栈：{1}",
                    ["MessageTips8"] = "加载语言设置时出错: {0}",
                    ["MessageTips9"] = "语言加载错误",



                    // ... 可以继续添加更多中文键值对
                },

                // 英文语言包
                ["en"] = new Dictionary<string, string>
                {
                    ["MessageTips1"] = "The software has been started, please confirm in the background.",
                    ["MessageTips2"] = "Initializing the logging system",
                    ["MessageTips3"] = "Setting up exception handling",
                    ["MessageTips4"] = "Reading configuration",
                    ["MessageTips5"] = "Initialization completed",
                    ["MessageTips6"] = "fatal error",
                    ["MessageTips7"] = "The program has encountered a major exception. Please contact the Jinyuan Automation Software Design Department.\r\nAbnormal information: {0}\r\nException Stack：{1}",
                    ["MessageTips8"] = "Error loading language settings: {0}",
                    ["MessageTips9"] = "Language loading error",
                    // ... 可以继续添加更多英文键值对
                },

                // 日文语言包
                ["ja"] = new Dictionary<string, string>
                {
                    ["MessageTips1"] = "ソフトウェアが起動しました。バックグラウンドで確認してください。",
                    ["MessageTips2"] = "ログシステムを初期化しています...",
                    ["MessageTips3"] = "例外処理を設定しています...",
                    ["MessageTips4"] = "構成を読み込み中...",
                    ["MessageTips5"] = "初期化完了",
                    ["MessageTips6"] = "致命的なエラー",
                    ["MessageTips7"] = "プログラムに大きな異常が発生した場合は、【金源自動化ソフトウェア設計部】に連絡してください。日本語の\r\n例外情報: {0}\r\n例外スタック：{1}",
                    ["MessageTips8"] = "言語設定のロードエラー: {0}",
                    ["MessageTips9"] = "言語ロードエラー",

                    // ... 可以继续添加更多日文键值对
                },

                // 马来语言包
                ["ms"] = new Dictionary<string, string>
                {
                    ["MessageTips1"] = "Perisian telah dimulakan, sila sahkan di latar belakang.",
                    ["MessageTips2"] = "Memulakan sistem log",
                    ["MessageTips3"] = "Menetapkan pengendalian pengecualian",
                    ["MessageTips4"] = "Membaca konfigurasi",
                    ["MessageTips5"] = "Pemulaan selesai",
                    ["MessageTips6"] = "Ralat Fatal",
                    ["MessageTips7"] = "Program telah menghadapi pengecualian utama. Sila hubungi Jabatan Ralat Perisian Automatisasi Jinyuan.\r\nMaklumat tidak normal: {0}\r\nStack Kecualian：{1}",
                    ["MessageTips8"] = "Ralat memuatkan tetapan bahasa: {0}",
                    ["MessageTips9"] = "Ralat memuatkan bahasa",


                },

                // 匈牙利语言包
                ["hu"] = new Dictionary<string, string>
                {
                    ["MessageTips1"] = "A szoftver elindult, kérjük, erősítse meg a háttérben.",
                    ["MessageTips2"] = "A naplózási rendszer indítása",
                    ["MessageTips3"] = "Kivételek kezelésének beállítása",
                    ["MessageTips4"] = "Olvasási beállítások",
                    ["MessageTips5"] = "Inicializálás befejezve",
                    ["MessageTips6"] = "Halálos hiba",
                    ["MessageTips7"] = "A program jelentős kivétellel szembesült. Kérjük, lépjen kapcsolatba a Jinyuan Automation Software Design Osztállyal. \r\nKóros információk: {0}\r\nKivételek stack：{1}",
                    ["MessageTips8"] = "Hiba történt a nyelvbeállítások betöltésével: {0}",
                    ["MessageTips9"] = "Nyelvbetöltési hiba",


                },

                // 越南语言包
                ["vi"] = new Dictionary<string, string>
                {
                    ["MessageTips1"] = "Phần mềm đã được khởi chạy, hãy xác nhận trong nền.",
                    ["MessageTips2"] = "Đang khởi chạy hệ thống nhật ký...",
                    ["MessageTips3"] = "Thiết lập xử lý ngoại lệ...",
                    ["MessageTips4"] = "Đang đọc cấu hình...",
                    ["MessageTips5"] = "Khởi tạo xong",
                    ["MessageTips6"] = "Lỗi nghiêm trọng",
                    ["MessageTips7"] = "Chương trình xuất hiện đại dị thường, xin liên hệ 【 Kim Nguyên tự động hóa phần mềm thiết kế bộ 】.\r\nThông tin ngoại lệ: {0}\r\nNgăn xếp ngoại lệ：{1}",
                    ["MessageTips8"] = "Gặp lỗi khi tải cài đặt ngôn ngữ: {0}",
                    ["MessageTips9"] = "Lỗi tải ngôn ngữ",


                }
                // 可以继续添加更多语言...
            };
        }

        /// <summary>
        /// 获取当前语言的文本
        /// </summary>
        /// <param name="key">文本键值</param>
        /// <returns>对应语言的文本，如果未找到则返回键值本身</returns>
        public static string GetText(string key, string _currentLanguage)
        {
            if (_languageData.ContainsKey(_currentLanguage) &&
                _languageData[_currentLanguage].ContainsKey(key))
            {
                return _languageData[_currentLanguage][key];
            }
            return key; // 如果找不到对应的文本，返回键值本身
        }

        /// <summary>
        /// 获取带变量的文本
        /// </summary>
        /// <param name="key">文本键值</param>
        /// <param name="args">要替换的变量值</param>
        /// <returns>替换变量后的文本</returns>
        public static string GetText(string key, string _currentLanguage, params object[] args)
        {
            try
            {
                if (_languageData.ContainsKey(_currentLanguage) &&
                    _languageData[_currentLanguage].ContainsKey(key))
                {
                    string format = _languageData[_currentLanguage][key];
                    if (args != null && args.Length > 0)
                    {
                        return string.Format(format, args);
                    }
                    return format;
                }
                return key;
            }
            catch (FormatException ex)
            {
                // 记录错误：格式字符串与参数不匹配
                Debug.WriteLine($"Language format error - Key: {key}, Error: {ex.Message}");
                return key;
            }
        }
    }
}
